//
//  CheckCodeView.m
//  YZMicroStock
//
//  Created by apple on 17/9/13.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "CheckCodeView.h"
#import <UIImageView+WebCache.h>

@interface CheckCodeView ()

@property (weak, nonatomic) IBOutlet UIView *viewContent;

@property (weak, nonatomic) IBOutlet UIImageView *imageViewPic;

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet UIView *viewOption;

@property (nonatomic, strong) NSString *strCode;

@property (weak, nonatomic) IBOutlet UILabel *labelTitle;

@property (weak, nonatomic) IBOutlet UIButton *btnCancle;

@property (weak, nonatomic) IBOutlet UIButton *btnSubmit;

@end

@implementation CheckCodeView

- (void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.5];
    [self joiningWithUrl];
    self.imageViewPic.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refreshPicture:)];
    [self.imageViewPic addGestureRecognizer:tap];
    self.labelTitle.textColor = [YZMAppStyle colorForMain];
    [YZMSetBorderAndCornerRadius setView:self.viewContent borderWidth:0 borderColor:nil cornerRadius:4.0f];
    [YZMSetBorderAndCornerRadius setView:self.textField borderWidth:1.0 borderColor:[YZMAppStyle colorForLine] cornerRadius:0.0];
    [YZMSetBorderAndCornerRadius addLineViewWithParentView:self.viewOption];
    [YZMSetBorderAndCornerRadius addVerLineViewWithView:self.btnCancle width:1 color:nil];
    [self.textField addTarget:self action:@selector(changeCode:) forControlEvents:UIControlEventEditingChanged];
    [self.textField becomeFirstResponder];
}

- (void)refreshPicture:(UITapGestureRecognizer *)tap{
    [self joiningWithUrl];
}

- (IBAction)clickedBtnCancle:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)clickedBtnSubmit:(id)sender {
    if ( !self.strCode || self.strCode.length == 0) {
        [MBProgressHUD showMessage:@"请先输入图片中的验证码"];
    }else{
        if (![self.strCode yz_isValidAuthCode]) {
            [MBProgressHUD showMessage:@"请输入4位数字验证码"];
        }else{
            if (self.delegate && [self.delegate respondsToSelector:@selector(checkCode:)]) {
                [self.delegate checkCode:self.strCode];
            }
        }
    }
}

- (void)changeCode:(UITextField *)textField{
    _strCode = textField.text;
}

- (void)joiningWithUrl {
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@?t=%@", URL_BASE_STRING, URL_FOR_PICAUTHURL, [NSDate date]];
    NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [self.imageViewPic sd_setImageWithURL:url placeholderImage:nil options:SDWebImageHandleCookies | SDWebImageCacheMemoryOnly];
}

- (void)show{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window).insets(insets);
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
